package com.sphd.emanu.fragment.topupdetail;


import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.emanu.R;
import com.sphd.emanu.adapter.TopupDetailAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.interfaces.OrderCountCallBack;
import com.sphd.emanu.interfaces.TopCountCallBack;
import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.login.Org;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.pojo.order.Order;
import com.sphd.emanu.pojo.vip.MemDeposit;
import com.sphd.emanu.utils.PermissionConfig;
import com.wzutils.BigDisMailUtils;
import com.wzutils.L;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @Author Hu
 * @Time 2017/4/18 -- 11:32.
 * @Email 87683202@qq.com
 */
public class TopupdetailFragment extends MVPBaseFragment<TopupdetailContract.View, TopupdetailPresenter> implements TopupdetailContract.View {
    int flag;
    Bundle bundle;
    User user;
    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Unbinder unbinder;
    TopupDetailAdapter topupDetailAdapter;
    TopCountCallBack topCountCallBack;
    boolean isCanDo;
    List<Moudle> moudles;

    public void setTopCountCallBack(TopCountCallBack topCountCallBack) {
        this.topCountCallBack = topCountCallBack;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_sale_topup_order;
    }

    @Override
    protected void initView() {
        unbinder = ButterKnife.bind(this, rootView);
        bundle = getArguments();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        moudles= (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo=isCanDo(moudles, PermissionConfig.MENU_ORDERS_3);
        setRefresh(mySwf);
        initEvent();
        initAdapter();
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(() -> initData());
        myRecycler.addOnItemTouchListener(new OnItemClickListener() {

            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {

            }

            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemChildClick(adapter, view, position);
                if (isCanDo){
                    MemDeposit memDeposit = (MemDeposit) adapter.getItem(position);
                    showProgressDialog();
                    mPresenter.enterDepositOrder(memDeposit.getId());
                }else {
                    toast(getString(R.string.no_perssion));
                }

            }
        });
    }

    private void initAdapter() {
        setRecyWithDecorationBySelf(myRecycler, R.color.red, R.dimen.dp_0_5);
        topupDetailAdapter = new TopupDetailAdapter(R.layout.item_top_up_detail, new ArrayList<MemDeposit>(), context, flag);
        topupDetailAdapter.setEmptyView(emptyView);
        myRecycler.setAdapter(topupDetailAdapter);
    }

    @Override
    protected void initData() {
        showProgressDialog();
        mPresenter.selectDepositOrder(user.getOid(), String.valueOf(flag));
    }


    public void setType(int type) {
        this.flag = type;
    }

    @Override
    public void recBean(Org org) {
        dissProgressDialog();
        topCountCallBack.callBackTopCount(org.getDepositOrderCount());

        topupDetailAdapter.setNewData(org.getDeposits());
    }

    @Override
    public void recRes(int res) {
        dissProgressDialog();
        if (res == 1) {
            toast(R.string.up_success);
            initData();
        } else {
            toast(R.string.up_failure);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
